package com.cx.controller;

import com.cx.pojo.*;
import com.cx.service.OrderService;
import com.cx.util.OrderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author ：zhangjie
 * @Description :
 */
@Controller
@RequestMapping("/order")
public class OrderDetailsController {

    @Autowired
    OrderService orderService;

    /**
     * 订单详情
     */
    @RequestMapping("/detail")
    public String goodDetails(Integer goodId,Integer number,HttpServletRequest request,HttpSession session){
        Users user = (Users) session.getAttribute("user");
        List<Address> addressList =orderService.selectByUserId(user.getId());
        GoodDetail goodDetail=orderService.selectById(goodId);
        BigDecimal price =goodDetail.getGoodPrice().multiply(BigDecimal.valueOf(number));

        OrderUtil orderUtils=new OrderUtil();
        Orders orders=new Orders();
        orders.setUserId(user.getId());
        String orderNo =orderUtils.getGeneratID();
        orders.setOrderNo(orderNo);
        orders.setOrderStatus(-2);
        orders.setTotalMoney(price);
        orders.setIsPay(0);
        int attfectRows1=orderService.insert(orders);
        List<Orders> ordersList =orderService.selectALL();
        OrderGoods orderGoods=new OrderGoods();
        orderGoods.setOrderId(ordersList.get(ordersList.size()-1).getId());
        orderGoods.setGoodId(goodId);
        orderGoods.setGoodNum(number);
        int attfectRows2=orderService.insert(orderGoods);
        request.getSession().setAttribute("orders",ordersList.get(ordersList.size()-1));

        request.getSession().setAttribute("addressList",addressList);
        request.getSession().setAttribute("goodDetail",goodDetail);
        request.getSession().setAttribute("number",number);
        request.getSession().setAttribute("price",price);
        return "orderDetail";
    }

    /**
     *地址添加
     */
    @RequestMapping("/address")
    public int Address(Address address,HttpSession session){
        Users user = (Users) session.getAttribute("user");
        address.setUserId(user.getId());
        return orderService.insert(address);
    }

    /**
     * 生成订单
     */
    @RequestMapping("/place")
    public String PlaceOrder(Integer addressId , Integer orderId, HttpServletRequest request, HttpSession session){
        Users user = (Users) session.getAttribute("user");
        Orders orders=orderService.selectByKey(orderId);
        orders.setAddressId(addressId);
        int attfectRows=orderService.updateByPrimaryKeySelective(orders);
        request.getSession().setAttribute("orders",orders);
        Address address=orderService.selectByPrimaryKey(addressId);
        request.getSession().setAttribute("address",address);
        return "orderPlace";
    }
}
